package com.jueban.Vo;

import com.jueban.model.ApplyInfo;
import com.jueban.model.ApplyOfIP;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Setter
@Getter
public class ApplyOfIPsVo extends ResultVo {

    List<ApplyOfIPPreview> applyOfIPPreviews;
    int offset;
    int pageNumber;
    int pageSize;

    @Data
    private class ApplyOfIPPreview {

        Integer id;
        String ipName;
        String finallyIpName;
        String applicant;
        Date createTime;

    }

    public ApplyOfIPsVo() {

    }



    private void setProperties(Page<ApplyOfIP> applyOfIPs) {
        pageSize = applyOfIPs.getSize();
        //pageNumber = (int) (applyOfIPs.getTotalElements() / pageSize);


        pageNumber = applyOfIPs.getTotalPages();
        offset = applyOfIPs.getNumber();
        applyOfIPPreviews = new ArrayList<ApplyOfIPPreview>();

        for (ApplyOfIP applyOfIP : applyOfIPs) {
            ApplyInfo applyInfo = applyOfIP.getApplyInfo();
            ApplyOfIPPreview applyOfIPPreview = new ApplyOfIPPreview();
            applyOfIPPreview.setApplicant(applyOfIP.getApplicant().getName());
            applyOfIPPreview.setCreateTime(applyOfIP.getCreateTime());
            applyOfIPPreview.setId(applyOfIP.getId());
            applyOfIPPreview.setIpName(applyInfo.getApplyIPName());
            applyOfIPPreview.setFinallyIpName(applyOfIP.getFinallyIPName());
            applyOfIPPreviews.add(applyOfIPPreview);
        }
    }

    public ApplyOfIPsVo(Page<ApplyOfIP> applyOfIPs) {
        setProperties(applyOfIPs);
    }

}
